package com.learning.batterySwapPro.common.response;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wangyunyun
 */
@Data
public class ApiResponse<T> implements Serializable {
	private int code;
	private String msg;
	private T data;
	private Map<String, Object> meta;
	private long timestamp;

	private ApiResponse(int code, String msg, T data) {
		this.code = code;
		this.msg = msg;
		this.data = data;
		this.timestamp = System.currentTimeMillis();
	}

	// 成功响应
	public static <T> ApiResponse<T> success() {
		return new ApiResponse<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null);
	}

	public static <T> ApiResponse<T> success(T data) {
		return new ApiResponse<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
	}

	public static <T> ApiResponse<T> success(String message, T data) {
		return new ApiResponse<>(ResultCode.SUCCESS.getCode(), message, data);
	}

	// 失败响应
	public static <T> ApiResponse<T> fail(int code, String msg) {
		return new ApiResponse<>(code, msg, null);
	}

	public static <T> ApiResponse<T> fail(int code, String msg, T errorData) {
		return new ApiResponse<>(code, msg, errorData);
	}

	// 添加元数据（用于分页等场景）
	public ApiResponse<T> addMeta(String key, Object value) {
		if (this.meta == null) {
			this.meta = new HashMap<>();
		}
		this.meta.put(key, value);
		return this;
	}
}